//
//  CoserDetailViewController.m
//  Double
//
//  Created by 袁庆秋 on 15/5/22.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "CoserDetailViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
@interface CoserDetailViewController ()

@end

@implementation CoserDetailViewController

- (void)dealloc
{
    [_name release];
    [_scroll release];
    [_picArray release];
    [_desc release];
    [_picUrl release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.picArray = [NSMutableArray array];
    }
    return self;
}
- (void)loadView
{
    [super loadView];
    [self startConnection];
    
    }

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark -
#pragma mark 数据请求
- (void)startConnection
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Activity&ios=23&pad=0&a=coserInfo&id=%@", self.picUrl];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        for (NSString *str1 in array) {
            [self.picArray addObject:str1];
        }
//        NSLog(@"picArray1 = %ld", (unsigned long)self.picArray.count);
        [self createView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
            NSLog(@"picArray2 = %ld", (unsigned long)self.picArray.count);
}

#pragma mark -
#pragma mark 创建页面视图
- (void)createView
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * self.picArray.count, self.view.frame.size.height - 64);
    self.scroll.pagingEnabled = YES;
    self.scroll.bounces = YES;
    [self.view addSubview:self.scroll];
    [self.scroll release];
    
#pragma mark -
#pragma mark 创建返回按钮
    
    
    for (int i = 0; i < self.picArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scroll.frame.size.width * i, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
        
        NSString *str = [self.picArray objectAtIndex:i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
        [self.scroll addSubview:imageView];
        
        UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height - 20, imageView.frame.size.width, 20)];
        descLabel.text = self.desc;
        descLabel.backgroundColor = [UIColor grayColor];
        [imageView addSubview:descLabel];
        
        [descLabel release];
        [imageView release];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
